// pages/index/index.js
const app = getApp()
import {wxRegister} from '../../utils/request'
const notice = require('../../apis/notice')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imageWidth: '',
    imageHeight: '',
    identity: '',
    dates: '',
    canIUseGetUserProfile: false,
    noticeList: [
      {
        id: 1,
        title: "绿水",
        content: "222222",
        publishTime: "2022/04/09 12:29:01",
        url: "https://it00zyq.oss-cn-beijing.aliyuncs.com/47d1f1a300c94c71a76ca612d2149a0d山.jpg"
      }
    ],
  },

  onSwiper(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../notice/notice?id=' + id
    })
  },

  async go(e) {
    const { index } = e.currentTarget.dataset
    let token = app.globalData.token
    let res;
    if (!token) {
      token = wx.getStorageSync('token').token
    }
    if (!token) {
      res = await wxRegister();
      if (res.code == 200) {
        this.goTopage(index)
      }
    } else {
      this.goTopage(index)
    }
  },

  async goTopage(index) {
    let userInfo = JSON.parse(wx.getStorageSync('userInfo'))    
    const identity = userInfo.identity;
    const dd = userInfo.disabled;

    if (dd) {
      wx.showToast({
        title: '您已被禁用',
        icon: 'none'
      })
      return ;
    }

    if (identity == 0 && index != 1) {
      wx.showToast({
        title: '请先进行认证',
        icon: 'none'
      })
      return ;
    }

    if (identity == 1 && index == 3) {
      wx.showToast({
        title: '仅回收员可进入',
        icon: 'none'
      })
      return ;
    } 

    switch (parseInt(index)) {
      case 1:
        this.navigateTo("/pages/myPage/myPage")
        break;
      case 2:
        this.navigateTo("/pages/publish/publish")
        break;
      case 3:
        this.navigateTo("/pages/orderHall/orderHall")
        break;
      case 4:
        this.navigateTo("/pages/myOrder/myOrder")
        break;
    }
  },

  async initData() {
    const result = await notice.listNotice();
    this.setData({
      noticeList: result.data,
      imageWidth: app.globalData.windowWidth - 14,
      imageHeight: (app.globalData.windowWidth - 14) * 3 / 4
    })
  },

  onLoad: function (options) {
    // 获取公告列表 
    this.initData();
    const { windowHeight, windowWidth } = app.globalData
    const backHeight = windowHeight - (windowWidth / 750) * 2 * 329.33
    this.setData({
      backHeight: backHeight
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  onReady: function () {



  },
  onHide(){
    
  },


  async onShow() {


  },
  async init() {


  },

  /**
     * 自定义路由
     * @param {*} obj 
     */
  async navigateTo(obj) {
    // await this.showSubscribeMessage()
    wx.navigateTo({
      url: obj
    })
  }

})